
import 'package:flutter/cupertino.dart';
import 'package:flutter_eve_plugin/constants/constant.dart';
import 'package:flutter_eve_plugin/constants/plot_trigger.dart';
import 'package:flutter_eve_plugin/model/ye_xiu/achieves/archieves_ability_detail_entity.dart';
import 'package:flutter_eve_plugin/model/ye_xiu/achieves/archieves_ability_entity.dart';
import 'package:flutter_eve_plugin/model/ye_xiu/achieves/archieves_form_entity.dart';
import 'package:flutter_eve_plugin/model/ye_xiu/achieves/archieves_request_entity.dart';
import 'package:flutter_eve_plugin/model/ye_xiu/achieves/archieves_role_entity.dart';
import 'package:flutter_eve_plugin/model/ye_xiu/achieves/archieves_weapon_entity.dart';
import 'package:flutter_eve_plugin/pages/main/actions.dart';
import 'package:flutter_eve_plugin/plugin/main.dart';
import 'package:flutter_eve_plugin/store/base_action.dart';
import 'package:flutter_eve_plugin/utils/print_util.dart';
import 'package:flutter_eve_plugin/utils/toast_util.dart';
import 'package:redux/redux.dart';
import 'package:redux_thunk/redux_thunk.dart';
import 'constant.dart';

achievesRoleBody(Store store, ArchievesRoleEntity model) {
  if (model == null) return;
  store.dispatch(BaseAction(type: YX_ACHIEVES_ROLE, data: model));
}

achievesWeaponBody(Store store, ArchievesWeaponEntity model) {
  if (model == null) return;
  store.dispatch(BaseAction(type: YX_ACHIEVES_WEAPON, data: model));
}

achievesWeaponFormBody(Store store, ArchievesFormEntity model) {
  if (model == null) return;
  store.dispatch(BaseAction(type: YX_ACHIEVES_FORM, data: model));
}

achievesPersonDataBody(Store store, ArchievesAbilityEntity model) {
  if (model == null) return;
  store.dispatch(BaseAction(type: YX_PERSON_DATA, data: model));
}

achievesAbilityBody(Store store, ArchievesAbilityDetailEntity model) {
  if (model == null) return;
  store.dispatch(BaseAction(type: YX_ABILITY_DETAIL, data: model));
}

ThunkAction initData() {
  return (Store store) async {
    initRoleData(store);
    initWeaponData(store);
  };
}

ThunkAction refreshRoleData() {
  return (Store store) async {
    initRoleData(store);
  };
}

ThunkAction refreshWeaponData() {
  return (Store store) async {
    initWeaponData(store);
  };
}

ThunkAction getWeaponFormData() {
  return (Store store) async {
    PluginEve.sendMessage(PluginConstant.httpYXWeaponForm,
            httpType: HttpType.GET)
        .then((data) => achievesWeaponFormBody(store, data));
  };
}

//材料来源
ThunkAction getItemSource(int goodsType, String goodsId) {
  return (Store store) async {
    PluginEve.sendMessage(PluginConstant.httpYXSource,
            param: {'goodsType': goodsType, 'goodsId': goodsId},
            httpType: HttpType.POST)
        .then((data) {
      logPrint('getItemSource ' + data.toString());
    });
  };
}

ThunkAction getPersonalData() {
  return (Store store) async {
    PluginEve.sendMessage(PluginConstant.httpYXPersonalData,
            httpType: HttpType.GET)
        .then((data) {
      logPrint('ArchievesAbilityEntity state $data');
          achievesPersonDataBody(store, data);});
  };
}

ThunkAction upgradeForm(BuildContext context) {
  return (Store store) async {
    PluginEve.sendMessage(PluginConstant.httpYXFormUpgrade,
            httpType: HttpType.POST,context: context)
        .then((data) {
          logPrint('upgradeForm receive');
      handleFromResult(data, store, context);
    });
  };
}

void handleFromResult(data, Store store, BuildContext context) {
  if (data == null) {
    return;
  }
  //WeaponFormInfo
//  logDebugPrint("handleFromResult weaponFormId = ${data.data.weaponFormId}");
//  logDebugPrint("handleFromResult formLevel = ${data.data.formLevel}");

  //千机伞解锁新的形态了
  store.dispatch(doTriggerPlot(PlotTrigger.plotTriggerType5,data?.data?.weaponFormId ??""));
  handleResult(store,context);
}

ThunkAction upgradeWeapon(BuildContext context) {
  return (Store store) async {
    PluginEve.sendMessage(PluginConstant.httpYXWeaponUpgrade,
            httpType: HttpType.POST,context: context)
        .then((data) {
      handleWeaponResult(data, store, context);
    });
  };
}

void handleWeaponResult(data, Store store, BuildContext context) {
  //WeaponUpgrade
  if (data == null) {
    return;
  }
  //千机伞也升到5级了
  store.dispatch(doTriggerPlot(PlotTrigger.plotTriggerType8,'${data?.data?.level}'));
  handleResult(store,context);
}

ThunkAction upgradeCard(BuildContext context, String cardId) {
  return (Store store) async {
    PluginEve.sendMessage(PluginConstant.httpYXCardUpgrade,
            param: {"cardId": cardId}, httpType: HttpType.POST,context: context)
        .then((data) {
      handleCardResult(data, store, context);
    });
  };
}

void handleCardResult(data, Store store, BuildContext context) {
  if (data == null) {
    return;
  }
  logDebugPrint("handleCardResult data = ${data.data}");
  logDebugPrint("handleCardResult cardId = ${data.data.cardId}");
  logDebugPrint("handleCardResult level = ${data.data.level}");

  //叶修也升到5级了
  String id2 = "${data.data.cardId},${data.data.level}";
  store.dispatch(doTriggerPlot(PlotTrigger.plotTriggerType11,id2));
  handleResult(store,context);

}

void handleResult( Store store, BuildContext context) {
//    BaseRes res = data;
//  if (res.code == 0) {
//全局拦截非0code 无需判断

    Toast.show(context, '升级成功');


    //升级成功 刷新武器和角色数据
    store.dispatch(refreshRoleData());
    store.dispatch(refreshWeaponData());
//  } else {
//    print('handleResult ${res.message}');
//    Toast.show(context, res.message);
//  }
}

ThunkAction getAblityData() {
  return (Store store) async {
    PluginEve.sendMessage(PluginConstant.httpYXAbilityDetail,
            param: ArchievesRequestEntity(
                pagination: ArchievesRequestPagination(page: 1, pageSize: 100),
                skillType: "学习",
                subType: '叶修属性'),
            httpType: HttpType.POST)
        .then((data) => achievesAbilityBody(store, data));
  };
}

void initWeaponData(Store store) {
  PluginEve.sendMessage(PluginConstant.httpYXAchievesWeapon,
          httpType: HttpType.GET)
      .then((data) => achievesWeaponBody(store, data));
}

void initRoleData(Store store) {
  PluginEve.sendMessage(PluginConstant.httpYXAchievesRole,
          httpType: HttpType.GET)
      .then((data) => achievesRoleBody(store, data));
}
